/**
 * 
 */
package com.selfdriving.utils;

import java.util.List;

public class StringUtil {
	/**
	 * 初始化StringBuffer
	 * 
	 * @param sb
	 * @return StringBuffer
	 */
	public static StringBuffer initStringBuffer(StringBuffer sb) {
		if (sb != null) {
			sb.delete(0, sb.length());
		} else {
			sb = new StringBuffer();
		}
		return sb;
	}

	/**
	 * 初始化String
	 * 
	 * @return
	 */
	public static String emptyValue() {
		return "";
	}

	/**
	 * 判断String是否为空
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isNullOrEmpty(String s) {
		return ((s == null) || ("".equals(s.trim()))) || "null".equals(s);
	}

	public static boolean startsWith(String s, String prefix, boolean ignoreCase) {
		if (s != null && prefix != null) {
			if (ignoreCase) {
				return s.toLowerCase().startsWith(prefix.toLowerCase());
			} else {
				return s.startsWith(prefix);
			}
		}
		return s == null && prefix == null;
	}

	public static boolean equals(String s1, String s2, boolean ignoreCase) {
		if (!StringUtil.isNullOrEmpty(s1) && !StringUtil.isNullOrEmpty(s2)) {
			if (ignoreCase) {
				return s1.equalsIgnoreCase(s2);
			} else {
				return s1.equals(s2);
			}
		} else {
			return ((StringUtil.isNullOrEmpty(s1) && StringUtil
					.isNullOrEmpty(s2)) ? true : false);
		}
	}

	public static String unquote(String s, String quote) {
		if (!StringUtil.isNullOrEmpty(s) && !StringUtil.isNullOrEmpty(quote)) {
			if (s.startsWith(quote) && s.equals(quote)) {
				return s.substring(1, s.length() - quote.length());
			}
		}
		return s;
	}

	public static String quote(String s, String quote) {
		if (!StringUtil.isNullOrEmpty(s) && !StringUtil.isNullOrEmpty(quote)) {
			return quote.concat(s).concat(quote);
		}
		return s;
	}

	public static long parseLong(String value, long defaultValue) {
		try {
			if (StringUtil.isNullOrEmpty(value)) {
				return defaultValue;
			}
			return Long.parseLong(value);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return defaultValue;
	}

	public static int parseInt(String value, int defaultValue) {
		try {
			if (StringUtil.isNullOrEmpty(value)) {
				return defaultValue;
			}
			return Integer.parseInt(value);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return defaultValue;
	}

	public static boolean isInteger(String value) {
		boolean success = false;
		if (!isNullOrEmpty(value)) {
			int length = value.length();
			for (int i = 0; i < length; i++) {
				char cr = value.charAt(i);
				if ('0' <= cr && cr <= '9') {
					success = true;

				} else {
					success = false;
					break;
				}
			}
		}
		return success;
	}

	public static String getParams(List<String> list) {
		StringBuffer sb = new StringBuffer();
		if (list != null && list.size() > 0) {
			final int size = list.size();
			for (int i = 0; i < size; i++) {
				sb.append(list.get(i));
				if (i < size - 1) {
					sb.append(",");
				}
			}
		}
		return sb.toString();
	}

	public static String convertStreamToString(java.io.InputStream is) {
		java.util.Scanner s = new java.util.Scanner(is, "UTF-8")
				.useDelimiter("\\A");
		return s.hasNext() ? s.next() : "";
	}
}